<template>
  <a-modal
    :title="formInfo.id==undefined?'新增':'修改'"
    :width="1200"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :destroyOnClose="true"
    @ok="handleCancel"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
        <a-row>

            <a-col :span="11">
                <a-card size="small" title="报告列表" style="min-height:600px">
                    <div>
                        报告项目：<a-input placeholder="项目名称/拼音码" v-model="queryParam1.itemName" @keyup.enter.native="querydataf" style="width: 280px;height: 30px;"></a-input>
                        <a-button
                                size="small"
                                class="ml10"
                                type="primary"
                                style="margin-left: 10px"
                                @click="querydataf">
                            查询
                        </a-button>
                        <a-button size="small" class="ml10"
                                  @click="() => {this.$objToReset(queryParam1),this.querydataf()}"
                                  icon="reload"
                                  style="margin-left: 8px">重置</a-button>
                    </div>
                    <div class="kktable mt5">
                        <a-table
                                :columns="columns2"
                                :data-source="data1"
                                size="middle"
                                :pagination="{ pageSize: 15 }"
                                :customRow="Rowclickss"
                                :scroll="{ y: 550 }">
                            <!-- <template slot="checkBoxRender" slot-scope="text,record">
                                <a-checkbox :checked="parseInt(record.isTrue)===0"></a-checkbox>
                            </template>-->
                        </a-table>
                    </div>
                    <hos-department-window ref="departCodeRef" @ok="departCodeOk"></hos-department-window>
                </a-card>
            </a-col>

            <a-col :span="1">

            </a-col>
            <a-col :span="11">
                <a-card
                        size="small"
                        :title="titles"
                        class="c-ml-6"
                        style="min-height:600px">
                    <div class="kktable mt5">
                        <a-table
                                ref="table"
                                :columns="columns"
                                :data-source="selectData"
                                size="middle"
                                :pagination="false"
                                :scroll="{ y: 550 }">
                            <template slot="actionDel" slot-scope="text,record">
                                <a @click="handleDelete(record)">
                                    <a-icon type="delete"/>移除</a>
                            </template>
                            <template slot="stbtime" slot-scope="text,record">
                                <a-input
                                        style="width: 80px"
                                        v-model="record.stbtime"
                                        v-enterNumber
                                        type="number" />
                            </template>
                        </a-table>
                    </div>
                </a-card>
            </a-col>
        </a-row>
    </a-spin>
  </a-modal>
</template>

<script>
  import { getAction, postAction, deleteAction } from '@/api/manage'

  export default {
    name: 'HosDeptDoctor',
    components: {
    },
    data () {
      return {
        visible: false,
        confirmLoading: false,
        orderItemId: undefined,
          titles: '已选报告列表',
          formInfo: {
              createTime: undefined,
              wx: ''
          },
          columns: [
              {
                  title: '报告项目名称',
                  dataIndex: 'itemName'
              },
              {
                  title: '报告项目编号',
                  dataIndex: 'itemCode'
              },
              {
                  title: '操作',
                  dataIndex: 'timeunit',
                  scopedSlots: { customRender: 'actionDel' }
              }
          ],
          columns2: [
            {
              title: '报告项目名称',
              dataIndex: 'itemName'
            },
            {
              title: '报告项目编号',
              dataIndex: 'itemCode'
            }
          ],
          selectData: [],
          data1:[],
          queryParam1:{},
          queryParam:{},
        url: {
          saveInfo: '/system/hosDepartment/save',
          getInfo: '/system/hosDepartment/info',
          list: '/system/healthSubItem/list',
          listdd:'/system/healthOrderSubItems/list',
          addInfo:'/system/healthOrderSubItems/save',
          delInfo:'/system/healthOrderSubItems/delete'
        }
      }
    },
    created () {
    },
    methods: {
        // 获取用户对应部门弹出框提交给返回的数据
        departCodeOk (formData) {
            console.log('ff',formData)
            const departCodeNames = []
            const departCodeKeys = []
            let departCodeNameString = ''
            for (let i = 0; i < formData.departIdList.length; i++) {
                departCodeKeys.push(formData.departIdList[i].value)
                departCodeNames.push(formData.departIdList[i].title)
                departCodeNameString = departCodeNames.join(',')
            }
            if (departCodeKeys.length > 0) {
                this.queryParam1.deptId = departCodeKeys[0]
                this.queryParam1.deptName = departCodeNameString
            }
            this.$forceUpdate()
        },
        departCodeSearch () {
            this.$refs.departCodeRef.add([this.queryParam1.deptId],2)
        },
        editorOk (val) {
            this.formInfo.remark = val
        },
      add (orderItem) {
        this.edit(orderItem)
      },
      edit (record) {
        //this.$objToOverride(this.formInfo, record)
        if (record.id) {
          // getAction(this.url.getInfo, { 'deptId': record.deptId }).then(res => {
          //   this.$nextTick(() => {
          //       this.formInfo = res.result.data
                this.orderItemId = record.id;
                this.querydata (record.id)
          //   })
          // })
        }
        this.querydataf ()
        this.visible = true
      },
        // 删除
        handleDelete (record) {
            for (const n of this.selectData) {
                if (record == n) {
                    deleteAction(this.url.delInfo, { ids: record.id }).then((resp) => {
                        this.$message.success('删除成功!')
                        this.selectData.splice(this.selectData.indexOf(n), 1)
                    })

                }
            }
        },
        querydata (id) {
            // console.info(this.queryParam);
            this.queryParam.pageNo = 1
            this.queryParam.pageSize = 1000
            this.queryParam.orderItemId = id
            postAction(this.url.listdd, this.queryParam)
                .then(res => {
                    // console.info(res);
                    this.selectData = res.result.data.records
                })
        },
        querydataf () {
            this.queryParam1.pageSize = 1000
            this.queryParam1.pageNo = 1
            postAction(this.url.list, this.queryParam1)
                .then(res => {
                    // console.info(res);
                    this.data1 = res.result.data.records
                })
        },
      // 确定
      handleSubmit () {
        // 触发表单验证
        this.confirmLoading = true
        this.$refs.ruleForm.validate(valid => {
          console.log(valid)
          if (valid) {
            postAction(this.url.saveInfo, this.formInfo).then(res => {
              this.$message.success(res.content)
              this.$emit('ok', this.formInfo)
              this.close()
            }).finally(() => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
        rowClicks (record, index) {
            return {
                on: {
                    click: () => {
                        // if (record.flag === 0) {
                        //     record.flag = 1
                        //     this.selectedRowKeys.splice(this.selectedRowKeys.indexOf(record.id), 1)
                        // } else {
                        //     record.flag = 0
                        //     this.selectedRowKeys.push(record.id)
                        // }
                    },
                    dblclick: () => {
                    }
                }
            }
        },
        Rowclickss (record, index) {
            return {
                on: {
                    click: () => {

                    },
                    dblclick: () => {
                        let items = ''
                        for (const re of this.selectData) {
                            items += re.subItemId + ','
                        }
                        if (items.includes(record.id)) {
                            this.$message.info('该项目已添加，不能重复添加')
                        } else {
                            const addform = {}
                            addform.subItemId = record.id
                            addform.orderItemId = this.orderItemId
                            postAction(this.url.addInfo, addform).then(res => {
                                this.$message.success('添加成功')
                                this.$emit('ok', this.formInfo)
                                const mp = {
                                  subItemId: record.id,
                                  itemCode : record.itemCode,
                                  itemName : record.itemName,
                                  id: res.result.data.id
                                }
                                this.selectData.push(mp)
                            }).finally(() => {
                                this.confirmLoading = false
                            })
                        }
                        this.selectedRows = []
                    }
                }
            }
        },
      // 关闭
      handleCancel () {
        this.close()
      },
      close () {
        // this.$refs.ruleForm.resetFields()
        this.$objToReset(this.queryParam1)
        this.$objToReset(this.formInfo)
        this.$emit('close')
        this.$emit('ok', this.formInfo)
        this.visible = false
      }

    }
  }
</script>
